<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>APP角色管理</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/ztree/css/demo.css" type="text/css">
	<jsp:include page="${pageContext.request.contextPath }/resource_path.jsp" flush="true"/>
	<!-- ztree的js和css -->
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/assets/ztree/css/zTreeStyle/zTreeStyle.css">
	<script type="text/javascript" src="${pageContext.request.contextPath }/assets/ztree/js/jquery.ztree.core.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/assets/ztree/js/jquery.ztree.excheck.js"></script>
	<script type="text/javascript" src="${pageContext.request.contextPath }/assets/ztree/js/jquery.ztree.exedit.js"></script>
</head>
<body class="layui-anim layui-anim-up">
<div class="x-nav">
	<span class="layui-breadcrumb">
 		<a href="">首页</a>
		<a>
			<cite>APP角色管理</cite>
		</a>
	</span>
   </div>
<div class="x-body">
	<div class="layui-row">
		<div class="layui-form layui-col-md12 x-so">
			<input name="display-check" type="text" name="keyword" id="keyword"  placeholder="请输入角色名称" autocomplete="off" class="layui-input">
			<button class="layui-btn layui-btn-normal" data-type="reload"><i class="layui-icon layui-icon-search"></i>检索</button>
		</div>
	</div>
	<xblock>
		<button class="layui-btn layui-btn-warm" id="add"><i class="layui-icon layui-icon-add-circle-fine"></i>新增</button>
	</xblock>
	<table class="layui-hide" id="test" lay-filter="test"></table>
	<script type="text/html" id="barDemo">
		<a class="layui-btn layui-btn-xs" lay-event="authorize"><i class="layui-icon layui-icon-edit">分配权限</i></a>
		<a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit">编辑</i></a>
	</script>
</div>
<div id="formDiv" hidden>
	<div class="box">
		<form class="layui-form layui-card-body" id="addform">
			<input hidden id="id" name="id">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">角色名称</label>
					<div class="layui-input-inline">
						<input lay-verify="roleName" class="layui-input" lay-filter="roleName" type="text" id="roleName" name="roleName" style="width: 375px">
						<span class="xing" style="margin-left: 390px;font-size: 24px;"></span>
					</div>
				</div>
				<!-- <div class="layui-inline" style="float: right;margin-right: 3em;margin-top: 10px;">
					<font style="color:red; font-size: 24px;">*</font>
				</div> -->
			</div>
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">角色描述</label>
					<div class="layui-input-inline">
						<textarea class="layui-textarea" id="description" name="description" style="width: 375px;"></textarea>
					</div>
				</div>
			</div>
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn layui-btn-primary" lay-submit lay-filter="addform" id="subform">提交</button>
					<button class="layui-btn layui-btn-primary" style="margin-left:240px">取消</button>
				</div>
			</div>
		</form>
	</div>
</div>

<!-- 授权弹框 -->
<div id="formDiv2" hidden>
	<div class="box">
		<form class="layui-form layui-card-body" id="addform2">
			<input id="roleId" name="roleId" hidden>  	
			<!-- <div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">权限</label>
					<div class="layui-input-inline">
						<ul class="ztree" id="ztree"></ul>
					</div>
				</div>
			</div> -->
			<table class="layui-table">
				<tr>
					<td>
						<input name="display-check" type="checkbox" value="收货作业,1" id="1" title="收货作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<!-- 
					<td>
						<input name="display-check" type="checkbox" value="批量收货,2" id="2" title="批量收货" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					--> 
					<td>
						<input name="display-check" type="checkbox" value="物料搜索,3" id="3" title="物料搜索" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="出库作业,4" id="4" title="出库作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="紧急出库,6" id="6" title="紧急出库" lay-skin="primary" lay-filter="oneChoose">
					</td>
				</tr>
				<tr>
					<!-- 
					<td>
						<input name="display-check" type="checkbox" value="批量出库,5" id="5" title="批量出库" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					-->
					<!-- 
					<td>
						<input name="display-check" type="checkbox" value="预警,7" id="7" title="预警" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					-->
					<td>
						<input name="display-check" type="checkbox" value="退库作业,8" id="8" title="退库作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="精准上架,9" id="9" title="精准上架" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="精准下架,10" id="10" title="精准下架" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="精准拆箱,15" id="15" title="精准拆箱" lay-skin="primary" lay-filter="oneChoose">
					</td>
				</tr>
				<!-- 
				<tr>
					<td>
						<input name="display-check" type="checkbox" value="批量上架,11" id="11" title="批量上架" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="批量下架,12" id="12" title="批量下架" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					
				</tr>
				-->
				<tr>
					<!-- 
					<td>
						<input name="display-check" type="checkbox" value="高位上架,13" id="13" title="高位上架" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="高位下架,14" id="14" title="高位下架" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					-->
					<td>
						<input name="display-check" type="checkbox" value="盘点作业,16" id="16" title="盘点作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="质检作业,17" id="17" title="质检作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="拆箱计划,18" id="18" title="拆箱计划" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="配货计划,19" id="19" title="配货计划" lay-skin="primary" lay-filter="oneChoose">
					</td>
				</tr>
				<tr>
					<td>
						<input name="display-check" type="checkbox" value="不良品转换,20" id="20" title="不良品转换" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="配送作业,23" id="23" title="配送作业" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<!-- 
					<td>
						<input name="display-check" type="checkbox" value="批量拆箱,21" id="21" title="批量拆箱" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="条码匹配,22" id="22" title="条码匹配" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					<td>
						<input name="display-check" type="checkbox" value="出库报表,25" id="25" title="出库报表" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="入库报表,26" id="26" title="入库报表" lay-skin="primary" lay-filter="oneChoose">
					</td>
					-->
				</tr>
				<!--
				<tr>
					<td>
						<input name="display-check" type="checkbox" value="线边库存,24" id="24" title="线边库存" lay-skin="primary" lay-filter="oneChoose">
					</td> 
				</tr>
				<tr>
					<td>
						<input name="display-check" type="checkbox" value="当日库存,27" id="27" title="当日库存" lay-skin="primary" lay-filter="oneChoose">
					</td>
					<td>
						<input name="display-check" type="checkbox" value="批量退库,28" id="28" title="批量退库" lay-skin="primary" lay-filter="oneChoose">
					</td> 
					<td></td>
					<td></td>
					<td></td>
				</tr>
				-->
				<tr>
					<td colspan="2">
						<button class="layui-btn" style="margin-left:120px" lay-submit lay-filter="subform" id="subform1">提交</button>
					</td>
					<td colspan="2">
						<button class="layui-btn layui-btn-primary" style="margin-left:120px">取消</button>
					</td>
				</tr>
			</table>
			<!-- 
			<div class="layui-form-item">
				<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="subform" id="subform1">确定</button>
					<button class="layui-btn layui-btn-primary" style="margin-left:100px">取消</button>
				</div>
			</div> 
			-->
		</form>
	</div>
</div>

<script>
layui.use(['table','layer','upload','form','laydate'], function(){
	var table = layui.table;
	var laydate = layui.laydate;
	var layer = layui.layer,
    form = layui.form;

	var $ = layui.jquery, active = {
			reload:function () {
				var keyword = $("#keyword").val();
				var strWhere = "status = 0 and isdel = 0 and roleName like '%"+keyword+"%' and ";
				table.reload('contenttable',{
					method:'get',
					where:{"strWhere": strWhere}
					,page: {
	                    curr: 1//重新从第 1 页开始
	                }
				});
			}
		}
	$('.layui-btn').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
	table.render({
	    elem: '#test'
	    ,url:'${pageContext.request.contextPath }/system/selectSysRoleList.do'
	    ,title: '角色数据表'
	    ,id :'contenttable'
	    ,limits:[10,20,30]
	    ,toolbar: '#toolbar'
	    ,cols: [[
	      {type: 'checkbox', fixed: 'left'}
	      ,{field:'', title:'序号', sort: true, type:'numbers', width: 60}
	      ,{field:'roleName', title:'角色名称'}
	      /* ,{field:'remark', title:'角色类型'} */
	      ,{field:'description', title:'角色说明'}
	      ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
	    ]]
	    ,page: true
	    ,done:function(){
	    	$('th').css({
                'background-color': '#009688', 'color': '#fff','font-weight':'bold',
            })
	    }
	});

	//头事件
    $("#add").click(function(){
   		layer.open({
		  type: 1 		//Page层类型
		  ,area: ['585px', ''] //宽  高
		  ,skin: 'alert-skin'
		  ,title: '新增角色'
		  ,shade: 0.6 	//遮罩透明度
		  ,shadeClose: true //点击遮罩关闭
		  ,maxmin: true //允许全屏最小化
		  ,anim: 1 		//0-6的动画形式，-1不开启
		  ,content: $('#formDiv')
		  ,success: function () {
			  document.getElementById("addform").reset();
		  }
		  ,end: function () {
			  var formDiv = document.getElementById('formDiv');
			  formDiv.style.display = '';
		  }
		});
    });
  	//监听复选框事件
	table.on('checkbox(test)',function(obj){
		if(obj.checked == true && obj.type == 'all'){ 
			//点击全选 
			$('.layui-table-body table.layui-table tbody tr').addClass('layui-table-click'); 
		}else if(obj.checked == false && obj.type == 'all'){ 
			//点击全不选 
			$('.layui-table-body table.layui-table tbody tr').removeClass('layui-table-click'); 
		}else if(obj.checked == true && obj.type == 'one'){ 
			//点击单行
			if(obj.checked == true){ 
				obj.tr.addClass('layui-table-click'); 
			}else{ 
				obj.tr.removeClass('layui-table-click'); 
			} 
		}else if(obj.checked == false && obj.type == 'one'){ 
			//点击全选之后点击单行 
			if(obj.tr.hasClass('layui-table-click')){
				obj.tr.removeClass('layui-table-click'); 
			} 
		}
	})
	//监听行工具事件
	table.on('tool(test)', function(obj){
		var data = obj.data;
		if(obj.event === 'del'){
		  layer.confirm('确定删除吗？', function(index){
		    var id = obj.data.id;
		    $.ajax({
		    	type:'post',
		    	url:'${pageContext.request.contextPath }/system/deleteRoleById.do',
		    	data:{"id":id},
		    	success:function(data){
		    		if(data>0){
		    			toastr.success("删除成功！");
		    			/* setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
							//关闭模态框
							// 父页面刷新
							window.location.reload();
						},2000); */
                        table.reload('contenttable',{
                            method:'get',
        	                page: {
        	                    curr: 1//重新从第 1 页开始
        	                }
                        });
		    		}else{
		    			toastr.warning("删除失败！");
		    		}
		    	}

		    })
		    layer.close(index);
		  });
		} else if(obj.event === 'edit'){
			layer.open({
				  type: 1 		//Page层类型
				  ,area: ['580px', ''] //宽  高
				  ,skin: 'alert-skin'
				  ,title: '修改角色'
				  ,shade: 0.6 	//遮罩透明度
				  ,shadeClose: true //点击遮罩关闭
				  ,maxmin: true //允许全屏最小化
				  ,anim: 1 		//0-6的动画形式，-1不开启
				  ,content: $('#formDiv')
				  ,success: function () {
					//回显表单数据
					for(var i=0;i<Object.entries(data).length;i++) {
						var id = '#' + Object.entries(data)[i][0];
						var text = Object.entries(data)[i][1];
						$(id).val(text);
					}
					//$("#remark").val(data.remark)
					form.render();
				  }	
				  ,end: function () {
				  	var formDiv = document.getElementById('formDiv');
				  	formDiv.style.display = '';
			  	  }
				});
		} else if(obj.event === 'authorize'){
			var roleId = obj.data.id;
			$("#roleId").val(roleId);
			layer.open({
				type: 1 		//Page层类型
				,area: ['700px', ''] //宽  高
				,skin: 'alert-skin'
				,title: '角色授权'
			  	,shade: 0.6 	//遮罩透明度
			  	,shadeClose: true //点击遮罩关闭
			  	,maxmin: true //允许全屏最小化
			  	,anim: 1 		//0-6的动画形式，-1不开启
			  	,content: $('#formDiv2')
		  	 	,success: function(data){
		  	 		/***************权限回显***************/
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath }/system/queryAppMenuByRoleId.do?roleId='+roleId,
                        //data:{"id":id},
                        success:function(data){
                            $('input[name=display-check]').prop('checked', false);
                            form.render('checkbox');
                            if(data != null){
                            	var appMenu = eval("("+data+")");
                            	for (var i = 0; i < appMenu.length; i++) {
									if (appMenu[i].sign == ($("#1").val().substring($("#1").val().indexOf(",") + 1))) {
										var check = document.getElementById("1");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#2").val().substring($("#2").val().indexOf(",") + 1))) {
										var check = document.getElementById("2");
										check.checked = true;
										//form.render();
									} */
									if (appMenu[i].sign == ($("#3").val().substring($("#3").val().indexOf(",") + 1))) {
										var check = document.getElementById("3");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#4").val().substring($("#4").val().indexOf(",") + 1))) {
										var check = document.getElementById("4");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#5").val().substring($("#5").val().indexOf(",") + 1))) {
										var check = document.getElementById("5");
										check.checked = true;
										//form.render();
									} */
									if (appMenu[i].sign == ($("#6").val().substring($("#6").val().indexOf(",") + 1))) {
										var check = document.getElementById("6");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#7").val().substring($("#7").val().indexOf(",") + 1))) {
										var check = document.getElementById("7");
										check.checked = true;
										//form.render();
									} */
									if (appMenu[i].sign == ($("#8").val().substring($("#8").val().indexOf(",") + 1))) {
										var check = document.getElementById("8");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#9").val().substring($("#9").val().indexOf(",") + 1))) {
										var check = document.getElementById("9");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#10").val().substring($("#10").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("10");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#11").val().substring($("#11").val().indexOf(",") + 1))) {
										var check = document.getElementById("11");
										check.checked = true;
										form.render();
									}
									if (appMenu[i].sign == ($("#12").val().substring($("#12").val().indexOf(",") + 1))) {
										var check = document.getElementById("12");
										check.checked = true;
										//form.render();
									} */
									/* if (appMenu[i].sign == ($("#13").val().substring($("#13").val().indexOf(",") + 1))) {
										var check = document.getElementById("13");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#14").val().substring($("#14").val().indexOf(",") + 1))) {
										var check = document.getElementById("14");
										check.checked = true;
										//form.render();
									} */
									if (appMenu[i].sign == ($("#15").val().substring($("#15").val().indexOf(",") + 1))) {
										var check = document.getElementById("15");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#16").val().substring($("#16").val().indexOf(",") + 1))) {
										var check = document.getElementById("16");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#17").val().substring($("#17").val().indexOf(",") + 1))) {
										var check = document.getElementById("17");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#18").val().substring($("#18").val().indexOf(",") + 1))) {
										var check = document.getElementById("18");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#19").val().substring($("#19").val().indexOf(",") + 1))) {
										var check = document.getElementById("19");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#20").val().substring($("#20").val().indexOf(",") + 1))) {
										var check = document.getElementById("20");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#21").val().substring($("#21").val().indexOf(",") + 1))) {
										var check = document.getElementById("21");
										check.checked = true;
										//form.render();
									}
									if (appMenu[i].sign == ($("#22").val().substring($("#22").val().indexOf(",") + 1))) {
										var check = document.getElementById("22");
										check.checked = true;
										//form.render();
									} */
									if (appMenu[i].sign == ($("#23").val().substring($("#23").val().indexOf(",") + 1))) {
										var check = document.getElementById("23");
										check.checked = true;
										//form.render();
									}
									/* if (appMenu[i].sign == ($("#24").val().substring($("#24").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("24");
                                        check.checked = true;
                                        //form.render();
                                    }
                                    if (appMenu[i].sign == ($("#25").val().substring($("#25").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("25");
                                        check.checked = true;
                                        //form.render();
                                    }
                                    if (appMenu[i].sign == ($("#26").val().substring($("#26").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("26");
                                        check.checked = true;
                                        //form.render();
                                    }
                                    if (appMenu[i].sign == ($("#27").val().substring($("#27").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("27");
                                        check.checked = true;
                                        //form.render();
                                    }
                                    if (appMenu[i].sign == ($("#28").val().substring($("#28").val().indexOf(",") + 1))) {
                                        var check = document.getElementById("28");
                                        check.checked = true;
                                        //form.render();
                                    } */
                                    form.render('checkbox');
								}
                            }else{
                                console.log("回显失败");
                            }

                        }

                    })
                    /**********************************/
		  			successOther(laydate,data);
		  			form.render();
				}
				,end: function () {
				  	var formDiv = document.getElementById('formDiv2');
			  		formDiv.style.display = '';
		  	  	}
			});
		}
	});
	/**
     * 表单校验
     */
    form.verify({
    	//value：表单的值、item：表单的DOM对象
    	roleName: function(value, item){
    		if(value == ''){
    			return '角色名称不能为空';
    		}
    	}
    });
    /**
     * 通用表单提交(AJAX方式)（新增）
     */
    form.on('submit(addform)', function (data) {
    	$("#subform").attr("disabled",true);
    	$.ajax({
    		url : '${pageContext.request.contextPath }/system/saveSysRole.do',
   			data : $('#addform').serialize(),
   			type : "post",
   			//dataType : "json",
			}).done(
			  		function(res) {
			  			var list = res.split('_');
			  			if (parseInt(list[0]) > 0) {
			  				if(list[1].indexOf('add') != -1){
			  					toastr.success('新增成功！');
			  				}
			  				if(list[1].indexOf('edit') != -1){
			  					toastr.success('修改成功！');
			  				}
			  				/* setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
			  					//关闭模态框
			  					// 父页面刷新
			  					window.location.reload();  
			  				},2000); */
			  				layer.closeAll();
							$("#subform").attr("disabled",false);
                            table.reload('contenttable',{
                                method:'get',
            	                page: {
            	                    curr: 1//重新从第 1 页开始
            	                }
                            });
			  			}
			  		}
			  	).fail(
			  		function(res) {
			  			var data = res.responseText;
			  			var list = data.split('_');
						if(list[1].indexOf('add') != -1){
							layer.close(1);
							toastr.error('新增失败！');
							$("#subform").attr("disabled",false);
						}
						if(list[1].indexOf('edit') != -1){
							layer.close(1);
							toastr.error('修改失败！');
							$("#subform").attr("disabled",false);
						}
			  		}
			  	)
			return false;
    });
    /**
     * 通用表单提交(AJAX方式)
     */
    form.on('submit(subform)', function (data) {
    	$("#subform1").attr("disabled",true);
    	var menuNameArr = [];
        $('input[type=checkbox]:checked').each(function() {
            menuNameArr.push($(this).val());
        });
        if(menuNameArr.length==0){
        	toastr.info('至少选择一个权限！');
        	/* setTimeout(function(){
               
            },1000); */
            $("#subform1").attr("disabled",false);
        	return false;
        }else{
        	
        	for(i=0;i<menuNameArr.length;i++){
        	console.log("*************"+menuNameArr[i])
        	
        	}	
        	
        $.ajax({
            type:'post',
            url:'${pageContext.request.contextPath }/system/insertAppUserRole.do',
            data:{"menuNameArr": menuNameArr, "roleId":$("#roleId").val()},

        }).done(
            function(res) {
                if (res > 0) {
					toastr.success('提交成功！');
                    /* setTimeout(function(){
                        window.location.reload();
                    },1000); */
					layer.closeAll();
                    $("#subform1").attr("disabled",false);
                    table.reload('contenttable',{
                        method:'get',
    	                page: {
    	                    curr: 1//重新从第 1 页开始
    	                }
                    });
                } else {
                    toastr.error('提交失败！');
                    /* setTimeout(function(){
                        window.location.reload();
                    },1000); */
                    layer.closeAll();
                    $("#subform1").attr("disabled",false);
                    table.reload('contenttable',{
                        method:'get',
    	                page: {
    	                    curr: 1//重新从第 1 页开始
    	                }
                    });
				}
            }
        ).fail(
            function(res) {
                if (res > 0) {
                    toastr.success('提交成功！');
                    /* setTimeout(function(){
                        window.location.reload();
                    },1000); */
                    layer.closeAll();
                    $("#subform1").attr("disabled",false);
                    table.reload('contenttable',{
                        method:'get',
    	                page: {
    	                    curr: 1//重新从第 1 页开始
    	                }
                    });
                } else {
                    toastr.error('提交失败！');
                    /* setTimeout(function(){
                        window.location.reload();
                    },1000); */
                    layer.closeAll();
                    $("#subform1").attr("disabled",false);
                    table.reload('contenttable',{
                        method:'get',
    	                page: {
    	                    curr: 1//重新从第 1 页开始
    	                }
                    });
                }
            }
        )
		return false;
        }
    });
    
});

$(document).on('click', '#clear', function() {
	//layer.msg('响应点击事件');
	// 清空全部选中的节点
	var treeObj = $.fn.zTree.getZTreeObj("ztree");
	treeObj.checkAllNodes(false)
});
toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
</script>

<script type="text/javascript">
var setting = {
	    data:{//表示tree的数据格式
	        simpleData:{
	            enable:true,//表示使用简单数据模式
	            idKey:"id",//设置之后id为在简单数据模式中的父子节点关联的桥梁
	            pidKey:"pId",//设置之后pid为在简单数据模式中的父子节点关联的桥梁和id互相对应
	            rootId:"null"//pid为null的表示根节点
	        }
	    },
	    view:{//表示tree的显示状态
	        selectMulti:false//表示禁止多选
	    },
	    check:{//表示tree的节点在点击时的相关设置
	        enable:true,//是否显示radio/checkbox
	        chkStyle:"checkbox",//值为checkbox或者radio表示
	        checkboxType:{p:"",s:""},//表示父子节点的联动效果
	        radioType:"level"//设置tree的分组
	    }, 
	    /* callback:{//表示tree的一些事件处理函数
	        onClick:handlerClick,
	    } */
	}
/* 弹出框后加载信息 */
function successOther(laydate,data){
	ztree(data);
}
function ztree(data){	
	//第一步，先获取所有的权限信息，用树结构进行展示
	var treeData = [];
	var strWhere = "status = 0 and isdel = 0 and pid = 0 and ";
	$.ajax({
		type:'post',
		url:'${pageContext.request.contextPath }/json/queryAllRole.json',
		data:{"strWhere": strWhere},
		success:function(data){
			var list = eval("("+data+")");
			for (var i = 0; i < list.length; i++) {
				var list2 = list[i].mList;
				var cNodes = new Array();
				for (var j = 0; j < list2.length; j++) {
					cNodes.push({name: list2[j].permissionName,id: list2[j].id,pId: list[i].id});
				}
				treeData.push({name: list[i].permissionName,id: list[i].id,pId: null,children: cNodes});
			}
		  	$.fn.zTree.init($("#ztree"),setting,treeData);
		 	// 第二步，根据要授权的角色的ID，获取它已有的权限
			if(data != null && data != ""){
				 var roleId = $('#roleId').val();
				  $.ajax({
		            type: "post",
		            data: {"roleId":roleId},
		            url: "${pageContext.request.contextPath }/system/selectAllRoleOperation.do",
		            dataType: 'JSON',
		            async: false,
		            success: function (arr) {
				        for(var i=0;i<arr.length;i++){ 
							var zTree_Menu = $.fn.zTree.getZTreeObj("ztree");
							//console.log(zTree_Menu);
							var node = zTree_Menu.getNodeByParam("id",arr[i].permissionId,null);
							//console.log(node)
							zTree_Menu.checkNode(node,true,true);//指定选中ID的节点
							zTree_Menu.expandNode(node, true, false);//指定选中ID节点展开
				      	} 
		         	}
				}); 
			}
		},
		error : function(msg) {
		    //alert('树加载异常!');
		}
	
	});
}
function getCheck(id){
	//在提交表单之前，使用ztree提供的方式获取选中的节点
    var treeObj = $.fn.zTree.getZTreeObj(id);//获得ztree对象
    var nodes = treeObj.getCheckedNodes(true);//在提交表单之前将选中的checkbox收集
    var array = new Array();
    for(var i=0;i<nodes.length;i++){
        var id = nodes[i].id;
        array.push(id);
    }
    var ids = array.join(",");
    return ids;
}

</script>
</body>
</html>